package com.egdigital.allzic.adswizz;

import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler {

	private static final String		TAG								= XMLHandler.class.getSimpleName(); // log
	public static final String		IMPRESSION						= "Impression";
	//
	public static final String		MEDIA_FILE						= "MediaFile";
	public static final String		MEDIA_FILE_DURATION				= "duration";
	//
	private static final String		TRACKING						= "Tracking";
	private static final String		TRACKING_EVENT					= "event";
	//
	public static final String		TRACKING_START					= "start";
	public static final String		TRACKING_FIRST_QUARTILE			= "firstQuartile";
	public static final String		TRACKING_MIDPOINT				= "midpoint";
	public static final String		TRACKING_THIRD_QUARTILE			= "thirdQuartile";
	public static final String		TRACKING_COMPLETE				= "complete";
	//
	private static final String		COMPANION						= "Companion";
	private static final String		COMPANION_SIZE					= "height";
	public static final String		COMPANION_IMAGE					= "StaticResource";
	public static final String		COMPANION_CLICK_THROUGHT		= "CompanionClickThrough";
	public static final String		COMPANION_TRACKING_EVENT		= "TrackingEvents";
	public static final String		COMPANION_TRACKING				= "Tracking";
	//
	public static String			COMPANION_ADS_TYPE				= "";
	public static final String		COMPANION_ADS_INTERSTITIAL		= "960";
	public static final String		COMPANION_ADS_BANNER			= "90";
	//
	private StringBuilder			mParsedString					= null;
	private HashMap<String, String>	mData;
	//
	private boolean					canSaveTrackingStart			= false;
	private boolean					canSaveTrackingFirstQuartile	= false;
	private boolean					canSaveTrackingMidPoint			= false;
	private boolean					canSaveTrackingThirdQuartile	= false;
	private boolean					canSaveTrackingComplete			= false;

	public XMLHandler() {
		super();
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		mData = new HashMap<String, String>();
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if (localName.equals(TRACKING) && attributes.getValue(TRACKING_EVENT) != null) { // start tracking event
			if (attributes.getValue(TRACKING_EVENT).equals(TRACKING_START))
				canSaveTrackingStart = true;
			else if (attributes.getValue(TRACKING_EVENT).equals(TRACKING_FIRST_QUARTILE))
				canSaveTrackingFirstQuartile = true;
			else if (attributes.getValue(TRACKING_EVENT).equals(TRACKING_MIDPOINT))
				canSaveTrackingMidPoint = true;
			else if (attributes.getValue(TRACKING_EVENT).equals(TRACKING_THIRD_QUARTILE))
				canSaveTrackingThirdQuartile = true;
			else if (attributes.getValue(TRACKING_EVENT).equals(TRACKING_COMPLETE))
				canSaveTrackingComplete = true;
		} else if (localName.equals(COMPANION)) { // start companion
			if (attributes.getValue(COMPANION_SIZE).equals(COMPANION_ADS_BANNER)) {
				mData.put(COMPANION_ADS_TYPE, COMPANION_ADS_BANNER);
			} else if (attributes.getValue(COMPANION_SIZE).equals(COMPANION_ADS_INTERSTITIAL)) {
				mData.put(COMPANION_ADS_TYPE, COMPANION_ADS_INTERSTITIAL);
			}
		}
		mParsedString = new StringBuilder();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);
		if (canSaveTrackingStart) { // parse tracking events
			mData.put(TRACKING_START, mParsedString.toString());
			canSaveTrackingStart = false;
		} else if (canSaveTrackingFirstQuartile) {
			mData.put(TRACKING_FIRST_QUARTILE, mParsedString.toString());
			canSaveTrackingFirstQuartile = false;
		} else if (canSaveTrackingMidPoint) {
			mData.put(TRACKING_MIDPOINT, mParsedString.toString());
			canSaveTrackingMidPoint = false;
		} else if (canSaveTrackingThirdQuartile) {
			mData.put(TRACKING_THIRD_QUARTILE, mParsedString.toString());
			canSaveTrackingThirdQuartile = false;
		} else if (canSaveTrackingComplete) {
			mData.put(TRACKING_COMPLETE, mParsedString.toString());
			canSaveTrackingComplete = false;
		}
		if (localName.equals(IMPRESSION)) { // parse impression
			mData.put(IMPRESSION, mParsedString.toString());
		} else if (localName.equals(MEDIA_FILE)) {
			mData.put(MEDIA_FILE, mParsedString.toString());
		}
		if (localName.equals(COMPANION_IMAGE)) { // parse companions
			mData.put(COMPANION_IMAGE, mParsedString.toString());
		} else if (localName.equals(COMPANION_CLICK_THROUGHT)) {
			mData.put(COMPANION_CLICK_THROUGHT, mParsedString.toString());
		} else if (localName.equals(COMPANION_TRACKING)) {
			mData.put(COMPANION_TRACKING, mParsedString.toString());
		}
	}

	@Override
	public void characters(char ch[], int start, int length) {
		mParsedString.append(ch, start, length);
	}

	/**
	 * Return a {@link HashMap}, it can be either <li>empty <li><code>null</code> <li>full <br>
	 * or contain only one element
	 * 
	 * @return Hashmap <key, value>
	 */
	public HashMap<String, String> getResults() {
		// Log.d(TAG, "Results : " + mData.toString());
		return mData;
	}
}
